package com.example.project.controller;

import com.example.project.entity.User;
import com.example.project.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user") //自定义路径
public class UserController {
    @Resource
    UserMapper userMapper;

    @GetMapping
    public List<User> getUser(){
        return userMapper.findAll();
    }

    @PostMapping
    public String addUser(@RequestBody User user){
        userMapper.save(user);
        return "成功";
    }

    @PutMapping
    public String updateUser(@RequestBody User user){
        userMapper.updateById(user);
        return "成功";
    }

    @DeleteMapping("/{id}") //可以在网址接口后面传入id
    public String deleteUser(@PathVariable("id")Long id){
        userMapper.deleteById(id);
        return "成功";
    }

    @GetMapping("/{id}") //传id进来
    public User findById(@PathVariable("id")Long id){
        return  userMapper.findById(id);
    }

    @RequestMapping(path = "/login", method=RequestMethod.GET) //传用户名和密码
    public String login(String username,String password){
        User user=userMapper.selectByUserNameAndPassword(username,password);
        if(user==null){
            return "失败";
        }
        return "成功";
    }
}
